<?php
namespace Admin\Controller;
use Lib\Controller\AdminBaseController;

class AuditController extends AdminBaseController{

    private $provinceModel;
    private $cityModel;
    private $hospitalModel;
    private $districtModel;
    private $nurseModel;

    public function __construct(){

        $this->provinceModel    = D('Province');
        $this->cityModel        = D('City');
        $this->districtModel    = D('District');
        $this->hospitalModel    = D('Hospital');                //医院
        $this->nurseModel       = D('Nurse');                   //护士
        parent::__construct();
    }

    /**
     * [month_card_list 月卡列表]
     * @author xiezy
     * @date   2017-11-24
     * @return [type]     [description]
     */
    public function hospital_list(){
        $where = null;
        if(!isset($p)){
            $p = '';
        }
        if (IS_POST) {
            //搜索省份
            if( !empty(I('post.search_province')) ){
                $orderNo = I('post.search_province',null,'int');
                $where['province_id'] = $orderNo;
            }
            //搜索城市
            if( !empty(I('post.search_city')) ){
                $orderNo = I('post.search_city',null,'int');
                $where['city_id'] = $orderNo;
            }

            //搜索医院名称
            if( !empty(I('post.keywords',null,'string')) ){
                $orderNo = I('post.keywords',null,'string');
                $where['hospital_name'] = array('like','%'.$orderNo.'%');
            }
        }
        //省份
        $province     = $this->provinceModel->select();
        
        //获取订单数量
        $hospitalNum = $this->hospitalModel->where($where)->count();
        
        //分页
        $p     = I('get.p',1);
        $page  = new \Lib\ORG\Page($hospitalNum);
        $limit = $page->listRows;
        $pageString  = $p.','. $limit;
        // 医院
        $hospitalList = $this->hospitalModel->where($where)->order('id asc')->page($p)->select();

        foreach($hospitalList as $key =>$val){
            if(isset($val['province_id'])){
                $province_name = $this->provinceModel->where('ProvinceID = '.$val['province_id'])->getField('ProvinceName');
                $hospitalList[$key]['province_name']   = $province_name;
            }
            if(isset($val['city_id'])){
                $city_name = $this->cityModel->where('CityID = '.$val['city_id'])->getField('CityName');
                $hospitalList[$key]['city_name']       = $city_name;
            }
            if(isset($val['district_id'])){
                $district_name = $this->districtModel->where('DistrictID = '.$val['district_id'])->getField('DistrictName');
                $hospitalList[$key]['district_name']       = $district_name;
            }
            //查看是否绑定唯一账户
            $user_name = $this->nurseModel->where('hospital_id = '.$val['id'])->getField('user_name');
            $hospitalList[$key]['user_name']       = $user_name;
        }

        $this->assign('province',$province);
        $this->assign('list',$hospitalList);
        $this->assign('page', $page->show());
        $this->display();
    }

    /**
     * [getCityByProvince 获取城市、区县]
     * @author xiezy
     * @date   2018-01-10
     * @return [type]     [description]
     */
    public function getCityByProvince(){
        $proVal              = I('post.proVal');
        $where['ProvinceID'] = $proVal;
        $city                = $this->cityModel->field('cityid,cityname')->where($where)->select();

        $this->ajaxReturn(array('status'=>1,'info'=>'','data'=>$city));
    }

    /**
     * [getCityByProvince 获取城市、区县]
     * @author xiezy
     * @date   2018-01-10
     * @return [type]     [description]
     */
    public function getDistrictByCity(){
        $cityVal             = I('post.cityVal');
        $where['CityID']     = $cityVal;
        $distric             = $this->districtModel->field('districtid,districtname')->where($where)->select();

        $this->ajaxReturn(array('status'=>1,'info'=>'','data'=>$distric));
    }

    /**
     * [add_hospital 添加医院 ]
     * @author xiezy
     * @date   2017-11-27
     */
    public function add_hospital(){

        $spData['province_id']      = I('post.province_id');    //省份id
        $spData['city_id']          = I('post.city_id');        //城市id
        $spData['district_id']      = I('post.district_id');    //医院id
        $spData['hospital_name']    = I('post.hospital_name');  //医院名称
        $spData['hospital_price']   = I('post.hospital_price');
        $spData['create_time']      = time();
        $spData['update_time']      = time();
        $spData['status']           = 1;
        $addNewCard = $this->hospitalModel->add($spData);
        if($addNewCard){
            $info['status'] = 1;
            $info['info'] = '添加成功';
            $info['url'] = U(MODULE_NAME.'/Audit/hospital_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '添加失敗';
            $this->ajaxReturn($info);
        }
    }

    /**
     * [bindingAccount 给每家医院绑定唯一账户--护士登录账户]
     * @author xiezy
     * @date   2018-07-02
     * @return [type]     [description]
     */
    public function bindingAccount(){
        $hData                  = I('post.');
        $hData['password']      = md5($hData['password']);
        $hData['create_time']   = time();
        $addNewCard = $this->nurseModel->add($hData);
        if($addNewCard){
            $info['status'] = 1;
            $info['info'] = '绑定成功';
            $info['url'] = U(MODULE_NAME.'/Audit/hospital_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '绑定失敗';
            $this->ajaxReturn($info);
        }
    }

    /**
     * [change_month_status 修改医院状态]
     * @author xiezy
     * @date   2017-12-02
     * @return [type]     [description]
     */
    public function change_hospital_status(){
        $where['id'] = I('get.id');
        $result = $this->hospitalModel->where($where)->find();

        $status = $result['status'] > 0 ? 0 : 1;
        $updateServerName = $this->hospitalModel->where($where)->save(array('status'=>$status,'update_time'=>time()));
        if($updateServerName){
            $info['status'] = 1;
            $info['info'] = '修改成功';
            $info['url'] = U(MODULE_NAME.'/Audit/hospital_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '修改失敗';
            $this->ajaxReturn($info);
        }
    }

    /**
     * [delete_month_list 删除月卡]
     * @author xiezy
     * @date   2017-12-02
     * @return [type]     [description]
     */
    public function delete_hospital() {
        $where['id'] = I('get.id');

        $deleteMonth = $this->hospitalModel->where($where)->delete();
        if($deleteMonth){
            $info['status'] = 1;
            $info['info'] = '刪除成功';
            $info['url'] = U(MODULE_NAME.'/Audit/hospital_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '刪除失敗';
            $this->ajaxReturn($info);
        }
    }


    public function admin_password_change(){
        if(IS_GET){
            $this->display();
        }
        if(IS_POST){
            $checkWhere['server_id'] = $where['server_id'] = -1;
            $checkWhere['admin_password'] = md5(I('post.old_pwd'));

            $checkResult = $this->serverAdminModel->where($checkWhere)->find();
            if(!$checkResult){
                $returndata['status'] = 0;
                $returndata['info'] = '原密碼不正確！';
                $this->ajaxReturn($returndata);
            }
            if(I('post.new_pwd') != I('post.confirm_new_pwd')){
                $returndata['status'] = 0;
                $returndata['info'] = '新密碼與確認密碼不一致！';
                $this->ajaxReturn($returndata);
            }

            $data['admin_password'] = md5(I('post.new_pwd'));
            $result = $this->serverAdminModel->where($where)->save($data);
            if($result){
                //清空session数据
                $_SESSION = array();

                //删除客户端cookie中保存的sessionid
                if (isset($_COOKIE[session_name()])) {
                    setcookie(session_name(), '', time() - 3600, C('COOKIE_PATH'));
                }

                //彻底销毁session
                session_destroy();

                S('menuList', NULL);
                S('allMenuId', NULL);
                S('language', NULL);
                S('currency', NULL);
                S('oauths', NULL);
                S('sysConfig', NULL);
                S('jsCacheNum', NULL);
                S('categoryList',NULL);


                $returndata['status'] = 1;
                $returndata['info'] = '修改成功！請重新登錄';
                $returndata['url'] = U(MODULE_NAME . "/Public/login", '', true, false, true, true);
                $this->ajaxReturn($returndata);
            }else{
                $returndata['status'] = 0;
                $returndata['info'] = '修改失敗！';
                $this->ajaxReturn($returndata);
            }
        }
    }

}
